
/**
 *
 * @param {Date} date
 * @returns  {string} 2021-01-01 00:00:00
 */
function getDateStr(date) {
  if (date === '') return ''
  const year = date.getFullYear().toString().padStart(4, '0')
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hour = date.getHours().toString().padStart(2, '0')
  const minute = date.getMinutes().toString().padStart(2, '0')
  const second = date.getSeconds().toString().padStart(2, '0')

  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}

/**
 * @return {number} 20210101
 */
function getDateYMStr(date) {
  if (date === '') return ''
  const year = date.getFullYear().toString().padStart(4, '0')
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')

  return parseInt(year + month + day)
}

/**
 * @return {string} 2021 01 01
 */
function getDateYMFormatStr(date) {
  if (date === '') return ''
  const year = date.getFullYear().toString().padStart(4, '0')
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  return year + ' ' + month + ' ' + day
}

/**
 *
 * @returns {string} 2021-01-01 00:00:00
 */
function getNowDateFull() {
  return getDateStr(new Date())
}

/**
 *
 * @param {number} pastHour
 * @returns  {string} 2021-01-01 00:00:00
 */
function getPastTimeFull(pastHour) {
  const date = new Date()
  date.setHours(date.getHours() - pastHour)
  return getDateStr(date)
}

export default {getDateStr, getDateYMStr, getNowDateFull, getPastTimeFull, getDateYMFormatStr}
